package bosanova
import groovy.sql.Sql
import org.springframework.dao.DataIntegrityViolationException
import grails.plugins.springsecurity.Secured
import org.codehaus.groovy.grails.plugins.springsecurity.SpringSecurityUtils

@Secured(['ROLE_ADMIN'])
class EmpleadoController {
    def springSecurityService
    def dataSource
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
    @Secured(['ROLE_ADMIN'])
    def infmaterias(){
        def result
        def db = new Sql(dataSource) // Create a new instance of groovy.sql.Sql with the DB of the Grails app
        result = db.rows("SELECT * FROM V_CANT_ESTUXMATERIA") // Perform the query       
        [ result: result ] 
    }
    def index() {
        redirect(action: "list", params: params)
    }

    def list() {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [empleadoInstanceList: Empleado.list(params), empleadoInstanceTotal: Empleado.count()]
    }

    def create() {
        [empleadoInstance: new Empleado(params)]
    }

    def save() {
        def empleadoInstance = new Empleado(params)
        def rol = Rol.findByAuthority(Authority.ROLE_PROFESOR.toString())
        
        if (!empleadoInstance.save(flush: true)) {          
            render(view: "create", model: [empleadoInstance: empleadoInstance])
            return
        }  
        UsuarioRol.create(empleadoInstance, rol)
        flash.message = message(code: 'default.created.message', args: [message(code: 'empleado.label', default: 'Empleado'), empleadoInstance.id])
        redirect(action: "show", id: empleadoInstance.id)
    }
    @Secured(['ROLE_PROFESOR','ROLE_ADMIN'])
    def show() {
        def empleadoInstance = Empleado.get(params.id)
        def isEmpleado = SpringSecurityUtils.ifAllGranted('ROLE_PROFESOR')
        if(isEmpleado){
            def username = springSecurityService.getCurrentUser()
            def entity = Empleado.find(username)
            if(!empleadoInstance){
                empleadoInstance = entity
            }
            if( !(entity.id == empleadoInstance.id) ){
                redirect(action: "index")
                return
            }        
        }
        if (!empleadoInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'empleado.label', default: 'Empleado'), params.id])
            redirect(action: "list")
            return
        }

        [empleadoInstance: empleadoInstance]
    }
    @Secured(['ROLE_PROFESOR','ROLE_ADMIN'])
    def edit() {
        def empleadoInstance = Empleado.get(params.id)
        if (!empleadoInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'empleado.label', default: 'Empleado'), params.id])
            redirect(action: "list")
            return
        }

        [empleadoInstance: empleadoInstance]
    }
    @Secured(['ROLE_PROFESOR','ROLE_ADMIN'])
    def update() {
        def empleadoInstance = Empleado.get(params.id)
        if (!empleadoInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'empleado.label', default: 'Empleado'), params.id])
            redirect(action: "list")
            return
        }

        if (params.version) {
            def version = params.version.toLong()
            if (empleadoInstance.version > version) {
                empleadoInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                    [message(code: 'empleado.label', default: 'Empleado')] as Object[],
                          "Another user has updated this Empleado while you were editing")
                render(view: "edit", model: [empleadoInstance: empleadoInstance])
                return
            }
        }

        empleadoInstance.properties = params

        if (!empleadoInstance.save(flush: true)) {
            render(view: "edit", model: [empleadoInstance: empleadoInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'empleado.label', default: 'Empleado'), empleadoInstance.id])
        redirect(action: "show", id: empleadoInstance.id)
    }

    def delete() {
        def empleadoInstance = Empleado.get(params.id)
        if (!empleadoInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'empleado.label', default: 'Empleado'), params.id])
            redirect(action: "list")
            return
        }

        try {
            empleadoInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'empleado.label', default: 'Empleado'), params.id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'empleado.label', default: 'Empleado'), params.id])
            redirect(action: "show", id: params.id)
        }
    }
}
